require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');

const app = express();
// 在 src/index.js 文件中添加以下代码

// 测试根路由
app.get('/', (req, res) => {
  res.send('Hello, World!');
});
// 中间件
app.use(cors());
app.use(express.json());

// 导入路由
const userRoutes = require('./routes/userRoutes');
const articleRoutes = require('./routes/articleRoutes');
const commentRoutes = require('./routes/commentRoutes');

// 使用路由
app.use('/api/users', userRoutes);
app.use('/api/articles', articleRoutes);
app.use('/api/articles', commentRoutes);

// 数据库连接
mongoose.connect(process.env.MONGO_URI)
.then(() => console.log('MongoDB connected'))
.catch(err => console.error('MongoDB connection error:', err));

// 启动服务器
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});
// 测试路由
app.get('/api/test', (req, res) => {
    res.json({ message: '测试路由正常工作' });
  });